package com.alin.location.set;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class Setting {
    private static  Context context = null;

    public class LocationData {
        long lat;  //纬度
        long lng;  //经度
        String addr;
    }

    public static SettingStruct dataObj;

    public static class SettingStruct {
        public String name;
        public List<LocationData> list;


        public String toString(){
            Gson gson = new Gson();
            String jsonString = gson.toJson(this);
            return jsonString;
        }
    }

    public static void Init(Context ctx){
        context = ctx;
        // 获取应用的内部存储目录
        File internalStorageDir = context.getFilesDir();

        Log.d(Enum.LOG_TAG, "setting init dir:" + internalStorageDir.toString());
        dataObj = new SettingStruct();

        File file = new File(context.getFilesDir(), "setting.json");
        if (file.exists()) {
            try {
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis);
                BufferedReader br = new BufferedReader(isr);
                StringBuilder content = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    content.append(line);
                }
                fis.close();

                Gson gson = new Gson();
                dataObj = gson.fromJson(content.toString(), SettingStruct.class);

                Log.d(Enum.LOG_TAG, dataObj.toString());

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("setting does not exist");
        }



    }



}
